package org.i0o.webplus.web.spring3.action;

import java.util.Locale;
import java.util.Map;

import org.i0o.webplus.commons.util.ValidateUtil;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

/**
 * 让spring mvc 支持多视图
 * 
 * @author <a href="mailto:781131@gmail.com">HTF</a>
 * @version 2010-11-25
 */
public class MultipleViewResolver
		implements ViewResolver {
	private Map<String, ViewResolver> resolvers;

	public void setResolvers(Map<String, ViewResolver> resolvers) {
		this.resolvers = resolvers;
	}

	@Override
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		int n = viewName.lastIndexOf(".");
		if (n == -1) {
			return null;
		}

		// 扩展名
		String suffix = viewName.substring(n + 1);

		ViewResolver resolver = resolvers.get(suffix);

		if (!ValidateUtil.isBlank(resolver)) {
			return resolver.resolveViewName(viewName, locale);
		}

		return null;
	}

}
